var lng;
var lat;
var bmap;

(function(e) {

	setPxPerRem();
	mui.plusReady(function() {

		lng = localStorage.getItem("lng");
		lat = localStorage.getItem("lat");
		var point;

		if(isEmpty(lng) || isEmpty(lat)) {
			point = new BMap.Point(120.389042, 36.073204);
			mui.toast("定位失败");
		} else {
			point = new BMap.Point(lng, lat);
		}
		bmap = new BMap.Map("mapDiv"); // 创建Map实例
		var convertor = new BMap.Convertor();
		var pointArr = [];
		pointArr.push(point);
		convertor.translate(pointArr, 1, 5, function(data) {
			var newPoint
			if(data.status === 0) {
				newPoint = data.points[0];
			} else {
				newPoint = point;
			}
			var geoc = new BMap.Geocoder();
			geoc.getLocation(newPoint, function(data1) {
				var addComp = data1.addressComponents;
				$("#addressStr").html(addComp.city + addComp.district + addComp.street + addComp.streetNumber);
			});
			bmap.centerAndZoom(newPoint, 12);
			bmap.setCurrentCity("青岛");
			bmap.disableScrollWheelZoom(true);
			if(isHaveNet()) {
				plus.nativeUI.showWaiting();
				getBuliderForPosition(newPoint.lat, newPoint.lng)
			} else {
				mui.toast("请检查网络连接后再试");
			}
		});
	});

})(mui);
var countNum = 0;

/**
 * 
 * @param {Object} latNum
 * @param {Object} lngNum
 */
function getBuliderForPosition(latNum, lngNum) {

	var jsonData = {
		building_category_id: "0",
		lat: latNum,
		lng: lngNum,
		distance: 10
	}

	var opts = {
		url: '/building/category/area/count',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	};

	crossDomainAjax(opts, function(data) {
		if(!isEmpty(data)) {
			var rows = data.data;
			if(!isEmpty(rows)) {
				var length = rows.length;
				if(rows.length > 3) {
					length = 3;
				}
				var createStr = "";

				for(var i = 0; i < length; i++) {

					countNum += rows[i].count;

					createStr = createStr + '<div id="' + rows[i].id + '" class="num-div">' +
						'<p>' + rows[i].name + '</p>' +
						'<p><span>' + rows[i].count + '</span>个</p></div>';
				}
				$("#countNum").html(countNum);
				getBuildPositionIcon(countNum, latNum, lngNum)
				$("#insertDiv").html(createStr);

				mui("#insertDiv").on("click", ".num-div", function() {
					bmap.clearOverlays();
					if(this.classList.contains("num-div-active")) {
						deleteClass("num-div-active", this);
						getBuildPositionIcon(countNum, latNum, lngNum)
					} else {
						changeType(this);
						var num = this.getElementsByTagName("span")[0].innerHTML;
						var categoryId = this.id;
						getBuildPositionIcon(num, latNum, lngNum, categoryId)
					}
				});
			} else {
				$("#countNum").html("0");
				showEle(document.getElementById("empty"))
			}
		}
	});
}

function changeType(ele) {
	var arrs = document.getElementById("insertDiv").getElementsByClassName("num-div");

	for(var i = 0; i < arrs.length; i++) {
		deleteClass("num-div-active", arrs[i]);
	}
	addClass('num-div-active', ele);
}

function getBuildPositionIcon(itemNum, latNum, lngNum, categoryId) {
	var jsonData = {
		page: 1,
		lat: latNum,
		lng: lngNum,
		distance: 10,
		per_page: itemNum
	}

	if(!isEmpty(categoryId)) {
		jsonData = {
			page: 1,
			lat: latNum,
			lng: lngNum,
			distance: 10,
			per_page: itemNum,
			building_category_id: categoryId
		}
	}

	plus.nativeUI.showWaiting();
	if(checkParams(jsonData)) {
		mui.toast("数据获取失败，请稍后再试");
		return;
	}

	var opts = {
		url: '/building',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	};

	crossDomainAjax(opts, function(data) {
		plus.nativeUI.closeWaiting();
		if(!isEmpty(data)) {
			var rows = data.data;
			if(!isEmpty(rows)) {
				for(var i = 0; i < rows.length; i++) {
					var pointLat;
					var pointLng
					if(!isEmpty(rows[i].lat) && !isEmpty(rows[i].lng)) {
						var point = new BMap.Point(rows[i].lng, rows[i].lat);
						point.id = rows[i].id;
						addMarker(point, rows[i].name, rows[i].building_category_id);
						document.getElementById("wwww").src = "../../ui/building/images/06searchaddress/red_position.png"
					}
				}
			} else {
				mui.toast("获取楼宇定位失败");
			}
		} else {
			mui.toast("获取楼宇定位失败");
		}
	});

}

function addMarker(point, title, type) {

	var myIcon = new BMap.Icon("../../ui/building/images/06searchaddress/red_position.png", new BMap.Size(32, 32));
	
	if(!isEmpty(type)){
		if(type == "10"){
			myIcon = new BMap.Icon("../../ui/building/images/06searchaddress/red_position.png", new BMap.Size(32, 32));
		} else if(type == "11"){
			myIcon = new BMap.Icon("../../ui/building/images/06searchaddress/blue_position.png", new BMap.Size(32, 32));
		} else {
			myIcon = new BMap.Icon("../../ui/building/images/06searchaddress/ye_position.png", new BMap.Size(32, 32));
		}
	}
	var marker = new BMap.Marker(point, {
		icon: myIcon
	});
	var label = new BMap.Label(title, {
		offset: new BMap.Size(20, -10)
	});

	label.setStyle("");
	marker.setLabel(label);
	bmap.addOverlay(marker);

	marker.addEventListener("click", function() {
		var postData = {
			pageflag: "page_searchAddress.html",
			itemID: point.id
		}
		var change = {
			pageUrl: 'page_buildinfo.html',
			postData: postData,
			aniShow: "fade-in"
		}
		pageChange(change);
	});

}